<?php


namespace app\api\controller\v17;

use app\api\controller\v2\Auth;
use app\common\lib\cache\CacheTools;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\response\Json;

/**
 * 档案列表和档案更新状态
 * @version 2.0.0
 * Class Recode
 * @package app\api\controller\v17
 * @method setUpdateStatus(int $uid)
 */
class Recode extends Auth
{
    /**
     * 查询用户档案列表
     * @param null $id
     * @return Json [type] [description]
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function index($id = NULL)
    {
        if ($id === NULL)
        {
            $id = input('id/d', 0);
        }
        $uid = $this->user['id'];
        if ($id)
        {
            $data = Db::table('app_recode')->alias('r')
                ->join('app_recode_astro ra', 'r.id=ra.rid', 'INNER')
                ->field('r.id,r.name,r.type,r.year,r.month,r.day,r.hour,r.minute,r.timezone,birth_country,birth_province,birth_city,live_country,live_province,live_city,comments,isself,long_deg,long_min,ew,lat_deg,lat_min,ns,avatar,is_summer,uid,aid,sun,moon,merc,venu,mars,jupi,satu,non,lili,juno,asc,mc,r.is_read,r.channel')
                ->where(['r.id' => $id, 'uid' => $uid])
                ->find();
            if (empty($data))
            {
                return json(['code' => 10322, 'msg' => '档案不存在']);
            }
        } else
        {
            $data = Db::table('app_recode')->alias('r')
                ->join('app_recode_astro ra', 'r.id=ra.rid', 'INNER')
                ->field('r.id,r.name,r.type,r.year,r.month,r.day,r.hour,r.minute,r.timezone,birth_country,birth_province,birth_city,live_country,live_province,live_city,comments,isself,long_deg,long_min,ew,lat_deg,lat_min,ns,avatar,is_summer,uid,aid,sun,moon,merc,venu,mars,jupi,satu,non,lili,juno,asc,mc,r.is_read,r.channel')
                ->where(['uid' => $uid, 'status' => 1])
                ->select();
        }
        //SWAN：百度小程序；WEAPP：微信小程序；QQ:QQ小程序；ALIPAY：支付宝小程序；TT：字节跳动小程序；H5：WEB
        foreach ($data as $k => $datum)
        {
            switch ($datum['channel'])
            {
                case 'SWAM':
                    $channel_des = '百度';
                    break;
                case 'WEAPP':
                    $channel_des = '微信';
                    break;
                case 'QQ':
                    $channel_des = 'QQ';
                    break;
                case 'ALIPAY':
                    $channel_des = '支付宝';
                    break;
                case 'TT':
                    $channel_des = '抖音';
                    break;
                case 'WEB':
                    $channel_des = 'H5';
                    break;
                default:
                    $channel_des = null;
                    break;
            }
            $data[$k]['channel_des'] = $channel_des;
            unset($data[$k]['channel']);
        }
        return json(['code' => 200, 'data' => $data, 'msg' => 'ok']);
    }

    /**
     * 获取更新状态
     * @throws \Exception
     */
    public function getUpdateStatus()
    {
        $uid = (int)$this->user['id'];

        $updateStatus = CacheTools::getRecodeUpdateStatus($uid);

        return show(config('code.success'), '请求成功', ['update_status' => $updateStatus]);
    }

    /**
     * 档案更新通知
     * @throws \Exception
     */
    public function updateNotify()
    {
        $uid = (int)$this->user['id'];

        CacheTools::setRecodeUpdateStatus($uid, 0);
//        (new Recode())->setUpdateStatus($uid);
        return show(config('code.success'), '请求成功');
    }
}
